package 深度优先搜索;

import common.TreeNode;

/**
 * 求根到叶子节点数字之和
 */
public class Solution129 {
    public int sumNumbers(TreeNode root) {
        if(root==null){
            return 0;
        }
        return getSum(0,root);
    }

    private int getSum(int temp,TreeNode root){
        temp=temp*10+root.val;
        if(root.left==null&&root.right==null){
            return temp;
        }
        if(root.left==null){
            return getSum(temp,root.right);
        }else if(root.right==null){
            return getSum(temp,root.left);
        }else {
            return getSum(temp,root.left)+getSum(temp,root.right);

        }
    }


}
